#include "mydownloadsdk.h"

MyDownLoadSDK::MyDownLoadSDK(QObject *parent) : QObject(parent)
{

}

void MyDownLoadSDK::Init()
{
    // Xunlei
    m_pXL_DLL = new QLibrary("xldl.dll", this);
    if( m_pXL_DLL->load() ){
        m_pXL_Init = (XL_Init)m_pXL_DLL->resolve("XL_Init");
        if(!m_pXL_Init()){
            qDebug()<<"XL_Init Failed!";
            return;
        }
        m_pXL_StopTask = (XL_StopTask)m_pXL_DLL->resolve("XL_StopTask");
        m_pXL_CreateTask = (XL_CreateTask)m_pXL_DLL->resolve("XL_CreateTask");
        m_pXL_StartTask = (XL_StartTask)m_pXL_DLL->resolve("XL_StartTask");
        m_pXL_DeleteTask = (XL_DeleteTask)m_pXL_DLL->resolve("XL_DeleteTask");
        m_pXL_QueryTaskInfoEx = (XL_QueryTaskInfoEx)m_pXL_DLL->resolve("XL_QueryTaskInfoEx");
        m_pXL_SetSpeedLimit = (XL_SetSpeedLimit)m_pXL_DLL->resolve("XL_SetSpeedLimit");
    }else{
        qDebug()<<"m_pXL_DLL Load failed!";
    }

    // Aira2

    // set dir
    SetDownDir("D:/DownLoad");
}

void MyDownLoadSDK::SetDownDir(QString strDir)
{
    m_strDownDir = strDir;
}

int MyDownLoadSDK::CreateTaskTh(QString strUrl)
{
    if (m_strDownDir.length() == 0){
        m_strDownDir = "D:/";
    }
    return CreateTaskTh2(strUrl, m_strDownDir, "");
}

int MyDownLoadSDK::CreateTaskTh2(QString strUrl, QString strSaveDir, QString strCookie)
{
    QUrl taskUrl = QUrl(strUrl);
    qDebug()<<"CreateTaskTh:"<<taskUrl.scheme()+"://"+taskUrl.authority()<<taskUrl.fileName();
    DownTaskParam params;
    // 任务URL
    std::wstring pStr = strUrl.toStdWString();
    wcsncpy(params.szTaskUrl, pStr.c_str(), pStr.length());
    // 引用页
    pStr = QString(taskUrl.scheme()+"://"+taskUrl.authority()).toStdWString();
    wcsncpy(params.szRefUrl, pStr.c_str(), pStr.length());
    // 浏览器cookie
    pStr = strCookie.toStdWString();
    wcsncpy(params.szCookies, pStr.c_str(), pStr.length());
    // 下载保存文件名.
    pStr = taskUrl.fileName().toStdWString();
    wcsncpy(params.szFilename, pStr.c_str(), pStr.length());
    // 文件保存目录
    pStr = strSaveDir.toStdWString();
    wcsncpy(params.szSavePath, pStr.c_str(), pStr.length());

    // creat task
    HANDLE hTaskID = m_pXL_CreateTask(params);
    m_pXL_StartTask(hTaskID);
    qDebug()<<"Create Task:"<<hTaskID<<(int)hTaskID;
    return (int)hTaskID;
}

bool MyDownLoadSDK::QueryTaskTh(int pTaskID, DownTaskInfo &taskInfo)
{
    return m_pXL_QueryTaskInfoEx((void*)pTaskID, taskInfo);
}

QString MyDownLoadSDK::QueryTaskTh2Json(int pTaskID)
{
    DownTaskInfo taskInfo;
    m_pXL_QueryTaskInfoEx((void*)pTaskID, taskInfo);
    //qDebug()<<"QueryTaskTh2Json:"<<taskInfo.nTotalSize<<taskInfo.fPercent<<taskInfo.nSpeed;
    QJsonObject retObj;
    retObj.insert("id", pTaskID);
    retObj.insert("size", taskInfo.nTotalSize);
    retObj.insert("percent", taskInfo.fPercent);
    retObj.insert("speed", taskInfo.nSpeed);
    retObj.insert("state", taskInfo.stat);
    retObj.insert("filename", QString::fromStdWString(taskInfo.szFilename));
    retObj.insert("srcnums",taskInfo.nSrcTotal);
    QJsonDocument retRoot;
    retRoot.setObject(retObj);
    return QString::fromUtf8(retRoot.toJson());
}

bool MyDownLoadSDK::DeleteTaskTh(int pTaskID)
{
    return m_pXL_DeleteTask((void*)pTaskID);
}

bool MyDownLoadSDK::StopTaskTh(int pTaskID)
{
    return m_pXL_StopTask((void*)pTaskID);
}

void MyDownLoadSDK::SetLimitSpeed(int nKBps)
{
    m_pXL_SetSpeedLimit(nKBps);
}
